
new Vue({
	el:'#myOrderPage',
	data(){

		return {
			openId:'',
			shareGroupOrderId: '',
			shareCourseId: '',
			courseTitle: '',
			orderList:[],
			courseItems: [
				{ 'name': 'Wonders--启蒙班', id: 10001 ,'info': '启蒙班：适合英语零基础的孩子'},
				{ 'name': 'Wonders--基础班', id: 10002 ,'info': '基础班：适合已有一年左右英语学习基础的孩子'},
				{ 'name': 'Wonders--进阶班', id: 10003 ,'info': '进阶班：适合已有两年及以上英语学习基础的孩子'},
			],
			isload:false,
			showShareTip:false,
			noOrder:false
		}
	},
	methods:{
		getUrlData(dataName){
			function getUrlVars(){ 
        var vars = {}, hash; 
        var href = window.location.href.split('#')[0];
        var hashes = href.slice(href.indexOf('?')+1).split('&'); 
        for(var i = 0; i < hashes.length; i++) { 
            hash = hashes[i].split('=');
            vars[hash[0]] = hash[1]?hash[1]:'';
        } 
        return vars; 
			} 
			var params = getUrlVars(); 
			
			for(var key in params){
					if(key == dataName){ 
							return params[key];
					}
			};
			return '';
		},
		getName(id){
			var courseItems = this.courseItems;
			for (var i = 0; i < courseItems.length;i++){
				if (courseItems[i].id == id){
					return courseItems[i].name;
				}
			}
		},
		goDetail(groupOrderId){
			location.href = 'group.html?groupOrderId='+groupOrderId;
		},
		orderSearch(openId){
			var self = this;

			if(!openId){ 
				self.noOrder = true;
				return;
			}

			$.ajax({
				url: 'https://api.svenglish.cn/api/order/my-orders?userId=' + openId,
				type: 'GET',
				success:function(data){
					if (data.code == 1) {
						var courses = data.data;
						for (var i = 0; i < courses.length; i++) {
							var courseName = self.getName(courses[i].courseId);
							courses[i].courseName = courseName;
						}
						self.orderList = courses;
						console.log(self.orderList);
						if(self.orderList.length==0){
							self.noOrder = true;
						}
					}
				}
			});	


		},
		shareFriend(){
			this.showShareTip = true;
		},
		isWeiXin(){
			var ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger'){
				return true;
			}else{
				return false;
			}
		},
  },
  watch:{
    
  },
	mounted() {


		var wxUserInfo = localStorage.getItem('wxUserInfo');
		if(wxUserInfo){
			var wxUserInfo = JSON.parse(wxUserInfo);
			this.openId = wxUserInfo.unionId;
		}else if(this.isWeiXin() && !wxUserInfo){
			location.href = 'login.html?url='+encodeURIComponent(location.href.split('#')[0]);
		}

		this.orderSearch(this.openId);

		
	}
});



